package com.yunxi.controller.sys;

import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.sys.pojo.RoleUserVO;
import com.yunxi.sys.service.RoleUserDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * Created by admin on 2018/8/13.
 */
@RestController
@RequestMapping("/admin/roleuser")
@Api(tags = "sys:roleUser", description = "用户分组管理接口")
public class RoleUserController {
    private static final Logger logger = LoggerFactory.getLogger(RoleUserController.class);
    @Autowired
    RoleUserDubboService roleUserDubboService;

    /**
     * 查询角色菜单列表
     * @param params
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询角色用户列表")
    public ApiResult list(@RequestParam Map<String,String> params) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
       return ResponseUtil.ok(roleUserDubboService.listByRoleId(params.get("roleId")));
    }

    /**
     * 查询角色菜单列表
     * @return
     */
    @GetMapping("/listByRoleId")
    @ApiOperation(value = "查询角色用户列表")
    public ApiResult listByRoleId(String roleId) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return ResponseUtil.ok(roleUserDubboService.listByRoleId(roleId));
    }

    @GetMapping("/listByRoleCode")
    @ApiOperation(value = "查询角色用户列表")
    public ApiResult listByRoleCode(String roleCode) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return ResponseUtil.ok(roleUserDubboService.listByRoleCode(roleCode));
    }

    /**
     */
    @GetMapping("/listByDomainId")
    @ApiOperation(value = "查询渠道商角色列表")
    public ApiResult listByDomainId(@RequestParam String domainId) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return ResponseUtil.ok(roleUserDubboService.listByDomainId(domainId));
    }

    /**
     */
    @GetMapping("/listByUserId")
    @ApiOperation(value = "查询渠道商角色列表")
    public ApiResult listByUserId(@RequestParam String userId) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return ResponseUtil.ok(roleUserDubboService.listByUserId(userId));
    }

    /**
     */
    @GetMapping("/listByAccount")
    @ApiOperation(value = "查询渠道商角色列表")
    public ApiResult listByAccount(@RequestParam String accountNo) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return ResponseUtil.ok(roleUserDubboService.listByAccount(accountNo));
    }

    /**
     *保存角色菜单
     * @return
     * @throws Exception
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存角色用户")
    public ApiResult save(@RequestBody RoleUserVO roleMenu, @CurrentUser LoginInfo user) throws Exception{
        roleUserDubboService.saveRoleUserList(roleMenu,user.getUserId());
        return ResponseUtil.ok("success");
    }

    /**
     *保存角色菜单
     * @return
     * @throws Exception
     */
    @PostMapping("/saveByRoleId")
    @ApiOperation(value = "保存角色用户")
    public ApiResult saveByRoleId(@RequestBody RoleUserVO roleUser, @CurrentUser LoginInfo user) throws Exception{
        roleUserDubboService.saveRoleUserList(roleUser);
        return ResponseUtil.ok("success");
    }
}
